/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('usercenter.service.user', [
        "ngResource"
    ])
    //用户地址服务
    .service('AddressService', ['$resource', 'AuthService', function ($resource, AuthService) {

        var userid = AuthService.getUserid();
        var UserContacts = $resource(uc_baseurl + 'usercontact');
        var UserContactsByUserid = $resource(uc_baseurl + 'usercontact/byuserid/:userid');
        var UserContactsById = $resource(uc_baseurl + 'usercontact/:id');
        //新增地址
        this.add_address = function (address,success) {
            address.userid = userid;
            return UserContacts.save(address,success);
        }

        //获取地址
        this.get_address = function () {
            if (userid != '') {
                return UserContactsByUserid.query({userid: userid});
            }
        }
        //通过地址id获取地址
        this.get_addressbyid = function (contactid) {
            return UserContactsById.get({id: contactid});
        }
        //删除收货地址
        this.remove = function (contactid) {
            var resource = $resource(uc_baseurl + "usercontact/:id");
            return resource.remove({id: contactid});
        }

        this.update = function (address) {
            var resource = $resource(uc_baseurl + "usercontact/byuserid/setdefault/:userid");
            return resource.get({userid: address.userid, contactid: address.contactid});
        }
        //修改收货地址
        this.revise = function (data) {
            var resource = $resource(uc_baseurl + "usercontact/:id");
            return resource.save({id: data.contactid}, data);
        }
        //获取省份
        this.getProvince = function(){
            return $resource("http://api.nonggui8.com/address/province").query();
        }
        //获取城市
        this.getCity = function(provinceId){
            return $resource("http://api.nonggui8.com/address/city/byprovince/:id").query({id:provinceId});
        }
        //获取县
        this.getCounty = function(cityId){
            return $resource("http://api.nonggui8.com/address/county/bycity/:id").query({id:cityId});
        }
    }])

